import {
    Body, ClassSerializerInterceptor,
    Controller,
    Get,
    Inject,
    Post,
    Query, UseInterceptors,
    UsePipes
} from "@nestjs/common";
import {SysUserService} from "../service/SysUserService";
import {SysUser} from "../entity/SysUser";
import {Result} from "../common/Result";
import {ApiOperation, ApiTags} from "@nestjs/swagger";
import {SkipAuth} from "../auth/guard/GlobalGuardProvider";
import {SysUserDto} from "../dto/SysUserDto";
import {PageQuery} from "../common/page/PageQuery";
import {PageResult} from "../common/page/PageResult";
import {MyValidationPipe} from "../common/pipe/MyValidationPipe";
import {Validate_Mods} from "../common/enum/ValidateModsEnum";
import {MyDtoSerializePipe} from "../common/pipe/MyDtoSerializePipe";

@ApiTags('用户模块')
@Controller('/sysUser')
export class SysUserController {
    @Inject()
    private sysUserService: SysUserService;


    /**
     * 注册用户
     * @param user
     */
    @ApiOperation({summary: '注册用户'})
    @SkipAuth()
    @Post('/register')
    async registerUser(@Body() user: SysUser): Promise<Result<any>> {
        return await this.sysUserService.registerUser(user);
    }

    /**
     * 添加用户
     * @param user
     */
    @ApiOperation({summary: '添加用户'})
    @Post('/add')
    async addUser(@Body() user: SysUserDto) {
        return await this.sysUserService.addUser(user);
    }

    /**
     * 编辑用户
     * @param user
     */
    @ApiOperation({summary: '编辑用户'})
    @UsePipes(new MyValidationPipe([Validate_Mods.update]))
    @Post('/edit')
    async editUser(@Body() user: SysUser): Promise<Result<any>> {
        return await this.sysUserService.editUser(user);
    }

    /**
     * 用户详情查询
     * @param user
     */
    @ApiOperation({summary: "用户详情查询"})
    @UsePipes(new MyValidationPipe([Validate_Mods.detail]))
    @Get("/details")
    async findDetails(@Body() user: SysUser): Promise<Result<SysUser>> {
        let sysUser = await this.sysUserService.findDetails(user);
        return Result.success("", sysUser)
    }

    /**
     * 用户详情查询2
     * @param query
     */
    @ApiOperation({summary: "用户详情查询2"})
    @UsePipes(new MyValidationPipe([Validate_Mods.detail]))
    @Get("/sysUserQuery")
    async userMsg(@Query() query: SysUserDto): Promise<Result<SysUser>> {
        let data = await this.sysUserService.sysUserQuery(query);
        return Result.success("用户详情查询2", data);
    }


    /**
     * 用户列表
     */
    @ApiOperation({summary: "用户列表查询"})
    @UsePipes(new MyDtoSerializePipe(SysUserDto))
    @Post("/userList")
    async userList(@Body() query: PageQuery<SysUserDto>) {
        let data = await this.sysUserService.userList(query);
        return Result.success("", PageResult.of(data));
    }

    /**
     * 删除用户
     */
    @ApiOperation({summary: "删除用户"})
    @UsePipes(new MyValidationPipe([Validate_Mods.delete]))
    @Post("/delete")
    async deleteUser(@Body() user: SysUser) {
        return await this.sysUserService.deleteUser(user);
    }

    /**
     * 修改用户
     * @param user
     */
    @ApiOperation({summary: "修改用户"})
    @UsePipes(new MyValidationPipe([Validate_Mods.update]))
    @Post("/update")
    async updateUser(@Body() user: SysUserDto) {
        return await this.sysUserService.updateUser(user);
    }

    /**
     * 测试Dto序列化和反序列化
     * @param user
     */
    @SkipAuth()
    @ApiOperation({summary: "测试Dto序列化和反序列化"})
    @Post("/test")
    testJson (@Body() user: SysUser) {
        // user.username = "xxxx";
        return Result.success("看看结果", user);
    }
}
